package trlygjj.tanruiliyigenjinjin.web.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import trlygjj.tanruiliyigenjinjin.domain.Attence;
import trlygjj.tanruiliyigenjinjin.page.PageResult;
import trlygjj.tanruiliyigenjinjin.query.AttenceQueryObject;
import trlygjj.tanruiliyigenjinjin.service.IAttenceService;
import trlygjj.tanruiliyigenjinjin.util.JsonResult;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;
import trlygjj.tanruiliyigenjinjin.util.SignInException;

import java.util.List;

/**
 * Created by user on 2018/7/11.
 */
@Controller
@RequestMapping("attence")
public class AttenceController {

    @Autowired
    private IAttenceService attenceService;

    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     *
     * @return
     */
    @RequiresPermissions("attence:index")
    @PermissionName("考勤页面")
    @RequestMapping("")
    public String index() {
        ;
        return "attence/list";
    }

    /**
     * 具有分页和高级查询
     *
     * @return
     */
    @RequiresPermissions("attence:index")
    @PermissionName("考勤数据")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(AttenceQueryObject qo) {
        PageResult result = attenceService.query(qo);
        return result;
    }

    /**
     * 查询全部
     *
     * @return
     */
    @RequiresPermissions("attence:listAll")
    @PermissionName("考勤全部数据")
    @RequestMapping("listAll")
    @ResponseBody
    public List<Attence> listAll() {
        return attenceService.selectAll();
    }

    /**
     * 删除
     *
     * @param attenceId 要删除的员工对象id
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("attence:delete")
    @PermissionName("考勤删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long attenceId) {
        JsonResult result = new JsonResult();
        try {
            attenceService.deleteByPrimaryKey(attenceId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 更新
     *
     * @param attence 要更新的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("attence:update")
    @PermissionName("考勤编辑")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Attence attence) {
        JsonResult result = new JsonResult();
        try {
            attenceService.updateByPrimaryKey(attence);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 保存
     *
     * @param attence 要保存的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("attence:save")
    @PermissionName("考勤保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Attence attence) {
        JsonResult result = new JsonResult();
        try {
            attenceService.insert(attence);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }


    /**
     * 做签到的功能
     *
     * @return 返回json
     */
    @RequiresPermissions("attence:signIn")
    @PermissionName("考勤签到")
    @RequestMapping("signIn")
    @ResponseBody
    public JsonResult signIn() {
        JsonResult result = new JsonResult();
        try {
            attenceService.signIn();
        } catch (SignInException e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("签到失败!");
        }
        return result;
    }

    /**
     * 做签退的功能
     *
     * @return 返回json
     */
    @RequiresPermissions("attence:signOut")
    @PermissionName("考勤签退")
    @RequestMapping("signOut")
    @ResponseBody
    public JsonResult signOut() {
        JsonResult result = new JsonResult();
        try {
            attenceService.signOut();
        } catch (SignInException e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("签退失败!");
        }
        return result;
    }
}
